/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VerReserva.java
 *
 * Created on 17-jun-2011, 23:17:47
 */
package guiBoleteria;

import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import java.util.List;
import logica.Fabrica;
import logica.IBoleteria;
import util.datatypes.DTTiposEntrada;
import util.datatypes.DTCliente;
import util.datatypes.DTFuncion;
import util.datatypes.DTPelicula;
import util.datatypes.DTReserva;
import util.exceptions.NoExisteClienteException;
import util.exceptions.NoExistePeliculaException;
import util.exceptions.NoExisteReservaException;

/**
 *
 * @author Administrador
 */
public class VerReserva extends javax.swing.JFrame {

    /** Creates new form VerReserva */
    private DTCliente cliente;
    private DTReserva reserva;
    private DTPelicula pelicula;
    private List<DTTiposEntrada> entradas;

    private void cargarDatosPelicula(DTFuncion f)
    {
        try
        {
            IBoleteria i = Fabrica.getIBoleteria();
            pelicula = new DTPelicula();
            pelicula = i.getFuncionPelicula(f);
        }
        catch(NullPointerException ex)
        {
                
        }
    }
    
    public VerReserva() {
        initComponents();
    }
    
    public VerReserva(DTReserva r, int ci) {

        initComponents();
        this.setLocationRelativeTo(null);

        try
        {
            reserva = r;
            IBoleteria i = Fabrica.getIBoleteria();

            if (reserva.isEstado() || reserva.isVendida() || !i.chequearReservaEnHora(reserva))
            {
                btnVenta.setEnabled(false);
                btnModificar.setEnabled(false);
                btnCancelarReserva.setEnabled(false);
            }

            entradas = reserva.getTiposEntradas();
            cliente = i.buscarCliente(ci);
            pelicula = new DTPelicula();

            cargarDatosPelicula(reserva.getFuncion());

            txtClient.setText(cliente.getNombre());
            txtFecha.setText(String.valueOf(reserva.getFecha()));
            txtPelicula.setText(pelicula.getNombre());
            txtFuncionInicio.setText(i.obtenerHoraString(reserva.getFuncion().getHoraInicio()));
            txtCostoTotal.setText(String.valueOf(reserva.getCostoTotal()));

            for(DTTiposEntrada e: entradas)
            {
                if (i.typeOfTipoComun(e))
                    txtTipoComun.setText(String.valueOf(e.getCantidadEntradas()));
                else if (i.typeOfTipoMenor(e))
                    txtTipoMenor.setText(String.valueOf(e.getCantidadEntradas()));
                else if (i.typeOfTipoMayor(e))
                    txtTipoMayor.setText(String.valueOf(e.getCantidadEntradas()));
            }

            chkCancelar.setSelected(reserva.isEstado());

            btnBuscarPeliculas.setVisible(false);
            btnBuscarFuncion.setVisible(false);
        }
        catch (NoExisteClienteException ex)
        {
            JOptionPane.showMessageDialog(null, "No se encontro el Cliente", "Error", JOptionPane.ERROR_MESSAGE);
        }
        catch(NumberFormatException ex)
        {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PanelTitulo = new javax.swing.JPanel();
        lblBienvenida = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        PanelContenido = new javax.swing.JPanel();
        PanelBotones = new javax.swing.JPanel();
        btnVenta = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btnCancelarReserva = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        PanelDatos = new javax.swing.JPanel();
        Izquierda = new javax.swing.JPanel();
        lblEmpty37 = new javax.swing.JLabel();
        lblEmpty36 = new javax.swing.JLabel();
        lblEmpty4 = new javax.swing.JLabel();
        lblClient1 = new javax.swing.JLabel();
        txtClient = new javax.swing.JTextField();
        lblEmpty1 = new javax.swing.JLabel();
        lblFecha1 = new javax.swing.JLabel();
        txtFecha = new javax.swing.JTextField();
        lblEmpty2 = new javax.swing.JLabel();
        lblPelicula1 = new javax.swing.JLabel();
        txtPelicula = new javax.swing.JTextField();
        btnBuscarPeliculas = new javax.swing.JButton();
        lblFuncion1 = new javax.swing.JLabel();
        txtFuncionInicio = new javax.swing.JTextField();
        btnBuscarFuncion = new javax.swing.JButton();
        lblTipos1 = new javax.swing.JLabel();
        lblMenor1 = new javax.swing.JLabel();
        txtTipoMenor = new javax.swing.JTextField();
        lblEmpty5 = new javax.swing.JLabel();
        lblComun1 = new javax.swing.JLabel();
        txtTipoComun = new javax.swing.JTextField();
        lblEmpty6 = new javax.swing.JLabel();
        lblMayor1 = new javax.swing.JLabel();
        txtTipoMayor = new javax.swing.JTextField();
        lblCosto1 = new javax.swing.JLabel();
        txtCostoTotal = new javax.swing.JTextField();
        lblEmpty7 = new javax.swing.JLabel();
        chkCancelar = new javax.swing.JCheckBox();
        lblEmpty38 = new javax.swing.JLabel();
        lblEmpty39 = new javax.swing.JLabel();
        lblEmpty40 = new javax.swing.JLabel();
        lblEmpty3 = new javax.swing.JLabel();
        Derecho = new javax.swing.JPanel();
        lblEmpty18 = new javax.swing.JLabel();
        lblEmpty17 = new javax.swing.JLabel();
        lblEmpty16 = new javax.swing.JLabel();
        lblEmpty8 = new javax.swing.JLabel();
        lblEmpty9 = new javax.swing.JLabel();
        lblEmpty10 = new javax.swing.JLabel();
        lblEmpty11 = new javax.swing.JLabel();
        lblEmpty13 = new javax.swing.JLabel();
        lblEmpty14 = new javax.swing.JLabel();
        lblEmpty12 = new javax.swing.JLabel();
        lblEmpty15 = new javax.swing.JLabel();
        lblEmpty19 = new javax.swing.JLabel();
        lblEmpty20 = new javax.swing.JLabel();
        lblEmpty21 = new javax.swing.JLabel();
        lblEmpty22 = new javax.swing.JLabel();
        lblEmpty23 = new javax.swing.JLabel();
        lblEmpty24 = new javax.swing.JLabel();
        lblEmpty25 = new javax.swing.JLabel();
        lblEmpty26 = new javax.swing.JLabel();
        lblEmpty27 = new javax.swing.JLabel();
        lblEmpty28 = new javax.swing.JLabel();
        lblEmpty29 = new javax.swing.JLabel();
        lblEmpty30 = new javax.swing.JLabel();
        lblEmpty31 = new javax.swing.JLabel();
        lblEmpty32 = new javax.swing.JLabel();
        lblEmpty33 = new javax.swing.JLabel();
        lblEmpty34 = new javax.swing.JLabel();
        lblEmpty35 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();

        setMinimumSize(new java.awt.Dimension(800, 600));

        PanelTitulo.setBackground(new java.awt.Color(222, 222, 237));
        PanelTitulo.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        PanelTitulo.setMinimumSize(new java.awt.Dimension(800, 600));
        PanelTitulo.setLayout(new java.awt.BorderLayout());

        lblBienvenida.setBackground(new java.awt.Color(255, 255, 255));
        lblBienvenida.setFont(new java.awt.Font("Arial", 1, 24));
        lblBienvenida.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblBienvenida.setText("Detalle de Reserva");
        lblBienvenida.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        lblBienvenida.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        PanelTitulo.add(lblBienvenida, java.awt.BorderLayout.CENTER);

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/pequeñocinema.png"))); // NOI18N
        jLabel8.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        PanelTitulo.add(jLabel8, java.awt.BorderLayout.LINE_START);

        getContentPane().add(PanelTitulo, java.awt.BorderLayout.NORTH);

        PanelContenido.setBackground(new java.awt.Color(192, 192, 213));
        PanelContenido.setFont(new java.awt.Font("Arial", 0, 11));
        PanelContenido.setMinimumSize(new java.awt.Dimension(10, 33));
        PanelContenido.setPreferredSize(new java.awt.Dimension(300, 246));
        PanelContenido.setLayout(new java.awt.BorderLayout());

        PanelBotones.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        PanelBotones.setLayout(new java.awt.GridLayout(1, 0));

        btnVenta.setText("Generar Entrada");
        btnVenta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVentaActionPerformed(evt);
            }
        });
        PanelBotones.add(btnVenta);

        btnModificar.setText("Modificar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });
        PanelBotones.add(btnModificar);

        btnCancelarReserva.setText("Cancelar Reserva");
        btnCancelarReserva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarReservaActionPerformed(evt);
            }
        });
        PanelBotones.add(btnCancelarReserva);

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/save_icon.jpg"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.setEnabled(false);
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });
        PanelBotones.add(btnGuardar);

        PanelContenido.add(PanelBotones, java.awt.BorderLayout.NORTH);

        PanelDatos.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        PanelDatos.setLayout(new java.awt.GridLayout(1, 2));

        Izquierda.setLayout(new java.awt.GridLayout(15, 2));
        Izquierda.add(lblEmpty37);
        Izquierda.add(lblEmpty36);
        Izquierda.add(lblEmpty4);

        lblClient1.setFont(new java.awt.Font("Arial", 0, 11));
        lblClient1.setText("Cliente:");
        Izquierda.add(lblClient1);

        txtClient.setEnabled(false);
        txtClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtClientActionPerformed(evt);
            }
        });
        Izquierda.add(txtClient);
        Izquierda.add(lblEmpty1);

        lblFecha1.setText("Fecha:");
        Izquierda.add(lblFecha1);

        txtFecha.setEnabled(false);
        Izquierda.add(txtFecha);
        Izquierda.add(lblEmpty2);

        lblPelicula1.setText("Pelicula:");
        Izquierda.add(lblPelicula1);

        txtPelicula.setEnabled(false);
        Izquierda.add(txtPelicula);

        btnBuscarPeliculas.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/pequeñoarrow_right.png"))); // NOI18N
        btnBuscarPeliculas.setEnabled(false);
        btnBuscarPeliculas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarPeliculasActionPerformed(evt);
            }
        });
        Izquierda.add(btnBuscarPeliculas);

        lblFuncion1.setText("Función:");
        Izquierda.add(lblFuncion1);

        txtFuncionInicio.setEnabled(false);
        Izquierda.add(txtFuncionInicio);

        btnBuscarFuncion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/pequeñoarrow_right.png"))); // NOI18N
        btnBuscarFuncion.setEnabled(false);
        btnBuscarFuncion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarFuncionActionPerformed(evt);
            }
        });
        Izquierda.add(btnBuscarFuncion);

        lblTipos1.setText("Tipos:");
        Izquierda.add(lblTipos1);

        lblMenor1.setText("Menor");
        Izquierda.add(lblMenor1);

        txtTipoMenor.setEnabled(false);
        txtTipoMenor.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtTipoMenorFocusLost(evt);
            }
        });
        txtTipoMenor.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtTipoMenorKeyTyped(evt);
            }
        });
        Izquierda.add(txtTipoMenor);
        Izquierda.add(lblEmpty5);

        lblComun1.setText("Común");
        Izquierda.add(lblComun1);

        txtTipoComun.setEnabled(false);
        txtTipoComun.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtTipoComunFocusLost(evt);
            }
        });
        txtTipoComun.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtTipoComunKeyTyped(evt);
            }
        });
        Izquierda.add(txtTipoComun);
        Izquierda.add(lblEmpty6);

        lblMayor1.setText("Mayor");
        Izquierda.add(lblMayor1);

        txtTipoMayor.setEnabled(false);
        txtTipoMayor.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtTipoMayorFocusLost(evt);
            }
        });
        txtTipoMayor.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtTipoMayorKeyTyped(evt);
            }
        });
        Izquierda.add(txtTipoMayor);

        lblCosto1.setText("Costo Total:");
        Izquierda.add(lblCosto1);

        txtCostoTotal.setEnabled(false);
        Izquierda.add(txtCostoTotal);
        Izquierda.add(lblEmpty7);

        chkCancelar.setText(" Cancelar");
        chkCancelar.setEnabled(false);
        chkCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkCancelarActionPerformed(evt);
            }
        });
        Izquierda.add(chkCancelar);
        Izquierda.add(lblEmpty38);
        Izquierda.add(lblEmpty39);
        Izquierda.add(lblEmpty40);
        Izquierda.add(lblEmpty3);

        PanelDatos.add(Izquierda);

        Derecho.setLayout(new java.awt.GridLayout(15, 2));
        Derecho.add(lblEmpty18);
        Derecho.add(lblEmpty17);
        Derecho.add(lblEmpty16);
        Derecho.add(lblEmpty8);
        Derecho.add(lblEmpty9);
        Derecho.add(lblEmpty10);
        Derecho.add(lblEmpty11);
        Derecho.add(lblEmpty13);
        Derecho.add(lblEmpty14);
        Derecho.add(lblEmpty12);
        Derecho.add(lblEmpty15);
        Derecho.add(lblEmpty19);
        Derecho.add(lblEmpty20);
        Derecho.add(lblEmpty21);
        Derecho.add(lblEmpty22);
        Derecho.add(lblEmpty23);
        Derecho.add(lblEmpty24);
        Derecho.add(lblEmpty25);
        Derecho.add(lblEmpty26);
        Derecho.add(lblEmpty27);
        Derecho.add(lblEmpty28);
        Derecho.add(lblEmpty29);
        Derecho.add(lblEmpty30);
        Derecho.add(lblEmpty31);
        Derecho.add(lblEmpty32);
        Derecho.add(lblEmpty33);
        Derecho.add(lblEmpty34);
        Derecho.add(lblEmpty35);

        PanelDatos.add(Derecho);

        PanelContenido.add(PanelDatos, java.awt.BorderLayout.CENTER);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 800, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        PanelContenido.add(jPanel1, java.awt.BorderLayout.PAGE_END);

        getContentPane().add(PanelContenido, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
    try
    {
        IBoleteria i = Fabrica.getIBoleteria();

        if (!ValidarControles())
            JOptionPane.showMessageDialog(null, "Debe ingresar todos los datos requeridos", "Error", JOptionPane.ERROR_MESSAGE);

        else if (!ValidarEntradas())
            JOptionPane.showMessageDialog(null, "La cantidad de entradas no puede ser superior a 5 ni inferior a 1", "Error", JOptionPane.ERROR_MESSAGE);

        else if (!ValidarCantidadEntradas())
            JOptionPane.showMessageDialog(null, "La disponibilidad de entradas para la funcion fue superada", "Error", JOptionPane.ERROR_MESSAGE);

        else if (!i.chequearFuncionesSolapadas(reserva.getFuncion(), cliente.getCi()))
            JOptionPane.showMessageDialog(null, "La funcion se superpone con otra reserva realizada por el cliente", "Error", JOptionPane.ERROR_MESSAGE);

        else if (!i.getChequearHoraFuncion(reserva.getFuncion()))
            JOptionPane.showMessageDialog(null, "Las reservas pueden ser realizadas solo hasta 30 minutos antes de que comience la función", "Error", JOptionPane.ERROR_MESSAGE);

        else if (reserva != null)
        {
            reserva.setCostoTotal(Double.parseDouble(txtCostoTotal.getText()));
            reserva.setEstado(chkCancelar.isSelected());
            reserva.setTiposEntradas(null);
            reserva.setTiposEntradas(entradas);

                if (i.modificarReservaCliente(reserva, cliente.getCi()) != 1)
                    JOptionPane.showMessageDialog(null, "Ha ocurrido un error y no se ha modificado la reserva", "Advertencia", JOptionPane.WARNING_MESSAGE);
                else
                {
                    setFormReadOnly();
                    btnCancelarReserva.setEnabled(true);
                    btnVenta.setEnabled(true);
                    btnModificar.setEnabled(true);
                    JOptionPane.showMessageDialog(null, "Se ha almacenado la reserva correctamente", "Exito", JOptionPane.INFORMATION_MESSAGE);
                }
        }
        else
            JOptionPane.showMessageDialog(null, "No existe la reserva", "Error", JOptionPane.ERROR_MESSAGE);
    }
    catch (NoExisteClienteException ex)
    {
        JOptionPane.showMessageDialog(null, "No existe el cliente", "Error", JOptionPane.ERROR_MESSAGE);
    }
    catch (NoExisteReservaException ex)
    {
        JOptionPane.showMessageDialog(null, "No existe la reserva", "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_btnGuardarActionPerformed

    private void setFormReadOnly()
    {
        txtPelicula.setEnabled(false);
        txtFuncionInicio.setEnabled(false);
        txtTipoMenor.setEnabled(false);
        txtTipoComun.setEnabled(false);
        txtTipoMayor.setEnabled(false);
        chkCancelar.setEnabled(false);
        btnBuscarPeliculas.setVisible(false);
        btnBuscarPeliculas.setEnabled(false);
        btnBuscarFuncion.setVisible(false);
        btnBuscarFuncion.setEnabled(false);
        btnGuardar.setEnabled(false);
    }
    
    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        txtPelicula.setEnabled(true);
        txtFuncionInicio.setEnabled(true);
        txtTipoMenor.setEnabled(true);
        txtTipoComun.setEnabled(true);
        txtTipoMayor.setEnabled(true);
        chkCancelar.setEnabled(true);
        btnBuscarPeliculas.setVisible(true);
        btnBuscarPeliculas.setEnabled(true);
        btnBuscarFuncion.setVisible(true);
        btnBuscarFuncion.setEnabled(true);
        btnGuardar.setEnabled(true);
        btnVenta.setEnabled(false);
        btnCancelarReserva.setEnabled(false);
        btnModificar.setEnabled(false);
    }//GEN-LAST:event_btnModificarActionPerformed

    private void txtClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtClientActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtClientActionPerformed

    private void btnCancelarReservaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarReservaActionPerformed

        int value = JOptionPane.showConfirmDialog(null, "Seguro que desea cancelar la reserva", "Pregunta", JOptionPane.YES_NO_OPTION);

        if (value == JOptionPane.YES_OPTION)
        {
            try
            {
                IBoleteria i = Fabrica.getIBoleteria();

                if (i.cancelarReservaCliente(reserva, cliente.getCi()) != 1)
                    JOptionPane.showMessageDialog(null, "Ha ocurrido un error y no se ha cancelado la reserva", "Error", JOptionPane.ERROR_MESSAGE);
                else
                {
                    setFormReadOnly();
                    btnModificar.setEnabled(false);
                    btnVenta.setEnabled(false);
                    JOptionPane.showMessageDialog(null, "La reserva ha sido cancelada", "Exito", JOptionPane.INFORMATION_MESSAGE);
                }
            }
            catch (NoExisteClienteException ex)
            {
                JOptionPane.showMessageDialog(null, "No existe el cliente", "Error", JOptionPane.ERROR_MESSAGE);
            }
            catch (NoExisteReservaException ex)
            {
                JOptionPane.showMessageDialog(null, "No existe la reserva", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_btnCancelarReservaActionPerformed

    private void chkCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkCancelarActionPerformed
        
    }//GEN-LAST:event_chkCancelarActionPerformed

    private void btnBuscarFuncionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarFuncionActionPerformed

        if (pelicula != null)
        {
            try
            {
                SeleccionarFuncion frm = new SeleccionarFuncion(null, true, pelicula);
                String id = frm.getValue();

                if (id == "")
                {
                    JOptionPane.showMessageDialog(null, "No ha modificado la función", "Advertencia", JOptionPane.INFORMATION_MESSAGE);
                }
                else
                {
                    int idFuncion = Integer.parseInt(id);
                    IBoleteria i =Fabrica.getIBoleteria();
                    reserva.setFuncion(i.getFuncionById(idFuncion, pelicula.getId()));
                    txtFuncionInicio.setText(i.obtenerHoraString(reserva.getFuncion().getHoraInicio()));
                }
            }
            catch (NoExistePeliculaException ex)
            {
                JOptionPane.showMessageDialog(null, "La pelicula o función seleccionadas no existen", "Error", JOptionPane.ERROR_MESSAGE);        
            }
            catch (NumberFormatException ex)
            {
                JOptionPane.showMessageDialog(null, "Ocurrio un error", "Error", JOptionPane.ERROR_MESSAGE);      
            }
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Debe seleccionar una pelicula", "Warning", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btnBuscarFuncionActionPerformed

    private void btnBuscarPeliculasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarPeliculasActionPerformed

        try
        {
            String id = new SeleccionarPelicula(null, true).getValue();

            if (id.equals(""))
            {
                JOptionPane.showMessageDialog(null, "No ha modificado la pelicula", "Advertencia", JOptionPane.INFORMATION_MESSAGE);
            }
            else
            {
                int idPelicula = Integer.parseInt(id);
                IBoleteria i =Fabrica.getIBoleteria();
                pelicula = i.getPelicula(idPelicula);
                txtPelicula.setText(pelicula.getNombre());
                txtFuncionInicio.setText("");
                JOptionPane.showMessageDialog(null, "Debe seleccionar uan función correspondiente a la pelicula seleccionada", "Warning", JOptionPane.WARNING_MESSAGE);
            }
        }
        catch (NumberFormatException ex)
        {
            JOptionPane.showMessageDialog(null, "No existe la pelicula seleccionada", "Error", JOptionPane.ERROR_MESSAGE);      
        }
    }//GEN-LAST:event_btnBuscarPeliculasActionPerformed

    private void txtTipoMenorKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTipoMenorKeyTyped

        char c = evt.getKeyChar();

        if (!((c >= '0') && (c <= '5') || (c == KeyEvent.VK_BACK_SPACE) ||  (c == KeyEvent.VK_DELETE))) 
        {
            getToolkit().beep();
            evt.consume();
        }
    }//GEN-LAST:event_txtTipoMenorKeyTyped

    private void txtTipoComunKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTipoComunKeyTyped

        char c = evt.getKeyChar();

        if (!((c >= '0') && (c <= '5') || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)))
        {
            getToolkit().beep();
            evt.consume();
        }
    }//GEN-LAST:event_txtTipoComunKeyTyped

    private void txtTipoMayorKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTipoMayorKeyTyped

        char c = evt.getKeyChar();

        if (!((c >= '0') && (c <= '5') || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)))
        {
            getToolkit().beep();
            evt.consume();
        }
    }//GEN-LAST:event_txtTipoMayorKeyTyped

    private void txtTipoMenorFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtTipoMenorFocusLost

        setValueToTipoMenor();
    }//GEN-LAST:event_txtTipoMenorFocusLost

    private void txtTipoComunFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtTipoComunFocusLost

        setValueToTipoComun();
    }//GEN-LAST:event_txtTipoComunFocusLost

    private void txtTipoMayorFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtTipoMayorFocusLost

        setValueToTipoMayor();
    }//GEN-LAST:event_txtTipoMayorFocusLost

    private void btnVentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVentaActionPerformed

        GenerarEntrada frm = new GenerarEntrada(reserva);
        frm.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_btnVentaActionPerformed
   
    private void setValueToTipoMayor()
    {
        try
        {
            IBoleteria i = Fabrica.getIBoleteria();

            double precioTotal = 0;
            int cantidadEntradas = 0;

            for(DTTiposEntrada e: entradas)
            {
                if (i.typeOfTipoMayor(e))
                    e.setCantidadEntradas(Integer.parseInt(txtTipoMayor.getText()));

                precioTotal += e.getCantidadEntradas() * e.getPrecioTipo();
                cantidadEntradas += e.getCantidadEntradas();
            }

            if (cantidadEntradas <= 5  && cantidadEntradas > 0)
                txtCostoTotal.setText(String.valueOf(precioTotal));

        }
        catch (Exception ex)
        {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void setValueToTipoMenor()
    {
        try
        {
            IBoleteria i = Fabrica.getIBoleteria();

            double precioTotal = 0;
            int cantidadEntradas = 0;

            for(DTTiposEntrada e: entradas)
            {
                if (i.typeOfTipoMenor(e))
                    e.setCantidadEntradas(Integer.parseInt(txtTipoMenor.getText()));

                precioTotal += e.getCantidadEntradas() * e.getPrecioTipo();
                cantidadEntradas += e.getCantidadEntradas();
            }

            if (cantidadEntradas <= 5  && cantidadEntradas > 0)
                txtCostoTotal.setText(String.valueOf(precioTotal));

        }
        catch (Exception ex)
        {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void setValueToTipoComun()
    {
        try
        {
            IBoleteria i = Fabrica.getIBoleteria();

            double precioTotal = 0;
            int cantidadEntradas = 0;

            for(DTTiposEntrada e: entradas)
            {
                if (i.typeOfTipoComun(e))
                    e.setCantidadEntradas(Integer.parseInt(txtTipoComun.getText()));

                precioTotal += e.getCantidadEntradas() * e.getPrecioTipo();
                cantidadEntradas += e.getCantidadEntradas();
            }

            if (cantidadEntradas <= 5 && cantidadEntradas > 0)
                txtCostoTotal.setText(String.valueOf(precioTotal));
        }
        catch (Exception ex)
        {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private boolean ValidarControles()
    {
        return (txtPelicula.getText().length() > 0 && txtFuncionInicio.getText().length() > 0
                && txtTipoComun.getText().length() > 0 && txtTipoMayor.getText().length() > 0
                && txtTipoMenor.getText().length() > 0);
    }
    
    private boolean ValidarEntradas()
    {
        return ((((Integer.parseInt(txtTipoComun.getText())) +
                (Integer.parseInt(txtTipoMenor.getText())) + (Integer.parseInt(txtTipoMayor.getText())))<=5)
                && (((Integer.parseInt(txtTipoComun.getText())) +
                (Integer.parseInt(txtTipoMenor.getText())) + (Integer.parseInt(txtTipoMayor.getText()))) > 0));
    }
    
    private boolean ValidarCantidadEntradas()
    {
        IBoleteria i = Fabrica.getIBoleteria();
        if (i.contarEntradas(reserva.getFuncion()) >= ((Integer.parseInt(txtTipoComun.getText())) +
                (Integer.parseInt(txtTipoMenor.getText())) + (Integer.parseInt(txtTipoMayor.getText()))))
            return true;
        return false;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new VerReserva().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel Derecho;
    private javax.swing.JPanel Izquierda;
    private javax.swing.JPanel PanelBotones;
    private javax.swing.JPanel PanelContenido;
    private javax.swing.JPanel PanelDatos;
    private javax.swing.JPanel PanelTitulo;
    private javax.swing.JButton btnBuscarFuncion;
    private javax.swing.JButton btnBuscarPeliculas;
    private javax.swing.JButton btnCancelarReserva;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JButton btnVenta;
    private javax.swing.JCheckBox chkCancelar;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblBienvenida;
    private javax.swing.JLabel lblClient1;
    private javax.swing.JLabel lblComun1;
    private javax.swing.JLabel lblCosto1;
    private javax.swing.JLabel lblEmpty1;
    private javax.swing.JLabel lblEmpty10;
    private javax.swing.JLabel lblEmpty11;
    private javax.swing.JLabel lblEmpty12;
    private javax.swing.JLabel lblEmpty13;
    private javax.swing.JLabel lblEmpty14;
    private javax.swing.JLabel lblEmpty15;
    private javax.swing.JLabel lblEmpty16;
    private javax.swing.JLabel lblEmpty17;
    private javax.swing.JLabel lblEmpty18;
    private javax.swing.JLabel lblEmpty19;
    private javax.swing.JLabel lblEmpty2;
    private javax.swing.JLabel lblEmpty20;
    private javax.swing.JLabel lblEmpty21;
    private javax.swing.JLabel lblEmpty22;
    private javax.swing.JLabel lblEmpty23;
    private javax.swing.JLabel lblEmpty24;
    private javax.swing.JLabel lblEmpty25;
    private javax.swing.JLabel lblEmpty26;
    private javax.swing.JLabel lblEmpty27;
    private javax.swing.JLabel lblEmpty28;
    private javax.swing.JLabel lblEmpty29;
    private javax.swing.JLabel lblEmpty3;
    private javax.swing.JLabel lblEmpty30;
    private javax.swing.JLabel lblEmpty31;
    private javax.swing.JLabel lblEmpty32;
    private javax.swing.JLabel lblEmpty33;
    private javax.swing.JLabel lblEmpty34;
    private javax.swing.JLabel lblEmpty35;
    private javax.swing.JLabel lblEmpty36;
    private javax.swing.JLabel lblEmpty37;
    private javax.swing.JLabel lblEmpty38;
    private javax.swing.JLabel lblEmpty39;
    private javax.swing.JLabel lblEmpty4;
    private javax.swing.JLabel lblEmpty40;
    private javax.swing.JLabel lblEmpty5;
    private javax.swing.JLabel lblEmpty6;
    private javax.swing.JLabel lblEmpty7;
    private javax.swing.JLabel lblEmpty8;
    private javax.swing.JLabel lblEmpty9;
    private javax.swing.JLabel lblFecha1;
    private javax.swing.JLabel lblFuncion1;
    private javax.swing.JLabel lblMayor1;
    private javax.swing.JLabel lblMenor1;
    private javax.swing.JLabel lblPelicula1;
    private javax.swing.JLabel lblTipos1;
    private javax.swing.JTextField txtClient;
    private javax.swing.JTextField txtCostoTotal;
    private javax.swing.JTextField txtFecha;
    private javax.swing.JTextField txtFuncionInicio;
    private javax.swing.JTextField txtPelicula;
    private javax.swing.JTextField txtTipoComun;
    private javax.swing.JTextField txtTipoMayor;
    private javax.swing.JTextField txtTipoMenor;
    // End of variables declaration//GEN-END:variables
}
